home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / think.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  7.5 KB  |  395 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28.  
  29. /* Ignore beyond this distance */
  30. #define THINK_CUTOFFA (20000.0 / KM_TO_UNITS1)
  31. #define THINK_CUTOFFA2 (THINK_CUTOFFA * THINK_CUTOFFA)
  32.  
  33. /* Pursue beyond this distance */
  34. #define THINK_CUTOFFB (2000.0 / KM_TO_UNITS1)
  35. #define THINK_CUTOFFB2 (THINK_CUTOFFB * THINK_CUTOFFB)
  36.  
  37. /* Maintain distance beyond this distance */
  38. #define THINK_CUTOFFC (500.0 / KM_TO_UNITS1)
  39. #define THINK_CUTOFFC2 (THINK_CUTOFFC * THINK_CUTOFFC)
  40.  
  41. void ThinkTarget (int t)
  42. /*
  43.  *  Give this target a chance to think about what it wants to do
  44.  */
  45. {
  46.   /* Targets do their own thinking in network games */
  47.   if (am_client || am_server) return;
  48.  
  49.   switch (target[t].strategy)
  50.   {
  51.     case STRAT_DONOTHING:
  52.     break;
  53.  
  54.     case STRAT_SIT1:
  55.     ThinkSit1 (t);
  56.     break;
  57.  
  58.     case STRAT_SIT2:
  59.     ThinkSit2 (t);
  60.     break;
  61.  
  62.     case STRAT_SIT3:
  63.     ThinkSit3 (t);
  64.     break;
  65.  
  66.     case STRAT_SIT4:
  67.     ThinkSit4 (t);
  68.     break;
  69.  
  70.     case STRAT_HUNT1:
  71.     ThinkHunt1 (t);
  72.     break;
  73.  
  74.     case STRAT_HUNT2:
  75.     ThinkHunt2 (t);
  76.     break;
  77.  
  78.     case STRAT_HUNT3:
  79.     ThinkHunt3 (t);
  80.     break;
  81.  
  82.     case STRAT_HUNT4:
  83.     ThinkHunt4 (t);
  84.     break;
  85.   }
  86. }
  87.  
  88. void ThinkSit1 (int t)
  89. /*
  90.  *  Don't move, just turn toward player and shoot
  91.  */
  92. {
  93.   TurnToward (t, player.pos);
  94. }
  95.  
  96. void ThinkSit2 (int t)
  97. /*
  98.  *  Don't move, just turn toward player and shoot
  99.  */
  100. {
  101.   double v[3];
  102.  
  103.   /* Aim at player */
  104.   if (!Aim (v, target[t].pos, target[t].vel, player.pos, player.vel,
  105.   weapon[target[t].weapon].speed))
  106.   {
  107.     TurnToward (t, player.pos);
  108.     return;
  109.   }
  110.  
  111.   TurnToward (t, v);
  112. }
  113.  
  114. void ThinkSit3 (int t)
  115. /*
  116.  *  Don't move, just turn toward nearest enemy and shoot poorly
  117.  */
  118. {
  119.   double pos[3], vel[3];
  120.  
  121.   if (!FindEnemy (t, pos, vel)) return;
  122.   TurnToward (t, pos);
  123. }
  124.  
  125. void ThinkSit4 (int t)
  126. /*
  127.  *  Don't move, just turn toward nearest enemy and shoot well
  128.  */
  129. {
  130.   double v[3], pos[3], vel[3];
  131.  
  132.   if (!FindEnemy (t, pos, vel)) return;
  133.   if (!Aim (v, target[t].pos, target[t].vel, pos, vel,
  134.   weapon[target[t].weapon].speed))
  135.   {
  136.     TurnToward (t, pos);
  137.     return;
  138.   }
  139.  
  140.   TurnToward (t, v);
  141. }
  142.  
  143. void ThinkHunt1 (int t)
  144. /*
  145.  *  Move toward player and shoot
  146.  */
  147. {
  148.   MoveToward (t, player.pos);
  149. }
  150.  
  151. void ThinkHunt2 (int t)
  152. /*
  153.  *  Move toward aiming point and shoot
  154.  */
  155. {
  156.   double v[3];
  157.  
  158.   /* Aim at player */
  159.   if (!Aim (v, target[t].pos, target[t].vel, player.pos, player.vel,
  160.   weapon[target[t].weapon].speed))
  161.   {
  162.     ThinkHunt1 (t);
  163.     return;
  164.   }
  165.  
  166.   MoveToward (t, v);
  167. }
  168.  
  169. void ThinkHunt3 (int t)
  170. /*
  171.  *  Move toward closest enemy and shoot poorly
  172.  */
  173. {
  174.   double pos[3], vel[3];
  175.  
  176.   if (!FindEnemy (t, pos, vel)) return;
  177.   MoveToward (t, pos);
  178. }
  179.  
  180. void ThinkHunt4 (int t)
  181. /*
  182.  *  Move toward closest enemy and shoot well
  183.  */
  184. {
  185.   double v[3], pos[3], vel[3];
  186.  
  187.   if (!FindEnemy (t, pos, vel)) return;
  188.   if (!Aim (v, target[t].pos, target[t].vel, pos, vel,
  189.   weapon[target[t].weapon].speed))
  190.   {
  191.     ThinkHunt3 (t);
  192.     return;
  193.   }
  194.  
  195.   MoveToward (t, v);
  196. }
  197.  
  198. void TurnToward (int t, double *pos)
  199. {
  200.   /*
  201.  *  Turn target t toward position v
  202.  */
  203.   double r, v[3], alpha, beta, theta;
  204.  
  205.   Vsub (v, pos, target[t].pos);
  206.   r = Mag2 (v);
  207.  
  208.   /* Don't bother if too far away */
  209.   if (r > THINK_CUTOFFA2)
  210.   {
  211.     target[t].vel[0] =
  212.     target[t].vel[1] =
  213.     target[t].vel[2] = 0.0;
  214.  
  215.     return;
  216.   }
  217.  
  218.   /* v is unit vector toward player */
  219.   Normalize (v);
  220.  
  221.   /* Determine angles from right-hand and up vectors */
  222.   alpha = Dotp (v, target[t].right);
  223.   beta = Dotp (v, target[t].up);
  224.   theta = Dotp (v, target[t].view);
  225.  
  226.   /* Turn towards player */
  227.  
  228.   /* First check left or right */
  229.   /* Is player to our right or left? */
  230.   if (alpha > 0.0)
  231.   {
  232.     /* To our right */
  233.     target[t].move_left = target[t].turnrate;
  234.   }
  235.   else
  236.   {
  237.     /* To our left */
  238.     target[t].move_right = target[t].turnrate;
  239.   }
  240.  
  241.   /* Up or down? */
  242.   if (beta > 0.0)
  243.   {
  244.     /* Above us */
  245.     target[t].move_up = target[t].turnrate;
  246.   }
  247.   else
  248.   {
  249.     target[t].move_down = target[t].turnrate;
  250.   }
  251.  
  252.   /* If we are facing target and are in range, shoot
  253.     a missile! */
  254.   if ( (theta > 0.9) && (r < weapon[target[t].weapon].range2) )
  255.   {
  256.     TargetFiresMissile (t);
  257.   }
  258. }
  259.  
  260. void MoveToward (int t, double *pos)
  261. {
  262.   /*
  263.  *  Turn target t toward position v
  264.  */
  265.   double r, v[3], alpha, beta, theta;
  266.  
  267.   Vsub (v, pos, target[t].pos);
  268.   r = Mag2 (v);
  269.  
  270.   /* Don't bother if too far away */
  271.   if (r > THINK_CUTOFFA2)
  272.   {
  273.     target[t].vel[0] =
  274.     target[t].vel[1] =
  275.     target[t].vel[2] = 0.0;
  276.  
  277.     return;
  278.   }
  279.  
  280.   /* v is unit vector toward player */
  281.   Normalize (v);
  282.  
  283.   /* Determine angles from right-hand and up vectors */
  284.   alpha = Dotp (v, target[t].right);
  285.   beta = Dotp (v, target[t].up);
  286.   theta = Dotp (v, target[t].view);
  287.  
  288.   /* Turn towards player */
  289.  
  290.   /* First check left or right */
  291.   /* Is player to our right or left? */
  292.   if (alpha > 0.0)
  293.   {
  294.     /* To our right */
  295.     target[t].move_left = target[t].turnrate;
  296.   }
  297.   else
  298.   {
  299.     /* To our left */
  300.     target[t].move_right = target[t].turnrate;
  301.   }
  302.  
  303.   /* Up or down? */
  304.   if (beta > 0.0)
  305.   {
  306.     /* Above us */
  307.     target[t].move_up = target[t].turnrate;
  308.   }
  309.   else
  310.   {
  311.     target[t].move_down = target[t].turnrate;
  312.   }
  313.  
  314.   /* Don't bother if too far away */
  315.   if (r > THINK_CUTOFFA2)
  316.   {
  317.     target[t].vel[0] = target[t].vel[1] = target[t].vel[2] = 0.0;
  318.   }
  319.  
  320.   /* If far enough away, move toward player */
  321.   if (r > THINK_CUTOFFB2)
  322.   {
  323.     if (theta > 0.0)
  324.     target[t].move_forward = target[t].maxvel;
  325.     else
  326.     target[t].move_backward = target[t].maxvel;
  327.   }
  328.   /* If too close, move away */
  329.   if (r < THINK_CUTOFFC2)
  330.   {
  331.     target[t].vel[0] = target[t].vel[1] = target[t].vel[2] = 0.0;
  332.   }
  333.  
  334.   /* If we are facing target and are in range, shoot
  335.     a missile! */
  336.   if ( (theta > 0.9) && (r < weapon[target[t].weapon].range2) )
  337.   {
  338.     TargetFiresMissile (t);
  339.   }
  340. }
  341.  
  342. int FindEnemy (int targ, double *pos, double *vel)
  343. /*
  344.  *  Find closest enemy to target targ, return position and speed
  345.  */
  346. {
  347.   int t, tt;
  348.   double d, v[3], r;
  349.  
  350.   /* d will be distance to closest */
  351.   d = -1.0;
  352.  
  353.   /* Loop through targets */
  354.   for (t=0; t<NTARGETS; t++)
  355.   {
  356.     if ( (target[t].age > 0.0) &&
  357.     (!target[t].hidden) &&
  358.     (t != targ) &&
  359.     (target[t].friendly != target[targ].friendly) )
  360.     {
  361.       /* Closer? */
  362.       Vsub (v, target[t].pos, target[targ].pos);
  363.       r = Mag2 (v);
  364.       if ( (d < 0.0) || (r < d) )
  365.       {
  366.         tt = t;
  367.         d = r;
  368.       }
  369.     }
  370.   }
  371.  
  372.   /* Set position and velocity if we found something */
  373.   if (d > 0.0)
  374.   {
  375.     Vset (pos, target[tt].pos);
  376.     Vset (vel, target[tt].vel);
  377.   }
  378.  
  379.   /* Must check player? */
  380.   if (!target[targ].friendly)
  381.   {
  382.     if ( (d < 0.0) || (target[targ].range2 < d) )
  383.     {
  384.       Vset (pos, player.pos);
  385.       Vset (vel, player.vel);
  386.       d = target[targ].range2;
  387.     }
  388.   }
  389.  
  390.   /* Found nothing? */
  391.   if (d == (-1.0)) return 0;
  392.  
  393.   return 1;
  394. }
  395.